import updateManager from './common/updateManager';
import {
  getOpenId,
  getUserInfo,
  createWechat
} from "./services/_utils/api"
import {
  SETSTORAGE,
  GETSTORAGE
} from "./services/storage/storage"
import {
  getFamilyMemberApi,
  getPersonInfoApi
} from "./services/filing/filing"
App({
  onLaunch: function (options) {

    if (options.path == 'pages/filing/InviteView/InviteView') {
      console.log('邀请');
      wx.login({
        success: res => {
          if (res.errMsg == 'login:ok') {
            const code = res.code
            getOpenId({
              code: code
            }).then(res => {
              SETSTORAGE('openid', res.data.openid)
              SETSTORAGE('unionid', res.data.unionid)
            })
          }
        }
      })
    } else {
      console.log('不是邀请');
      wx.login({
        success: res => {
          if (res.errMsg == 'login:ok') {
            const code = res.code;
            getOpenId({
              code: code
            }).then(res => {
              console.log(res, 'openid');
              var openid = res.data.openid //openid
              var unionid = res.data.unionid //openid
              SETSTORAGE('openid', openid)
              SETSTORAGE('unionid', unionid)
              // 获取用户信息
              getUserInfo({
                wechatMiniprogramOpenId: openid,
                userId: '',
                wechatServiceOfficialAccountOpenId: '',
                wechatUnionId: ''
              }).then(res => {
                let obj = {
                  miniprogramOpenid: openid, //小程序openid
                  unionid: unionid,
                  serviceOfficialAccountOpenid: ''
                }
                if (res.data.data == null || res.data.data == '') {
                  createWechat(obj).then(res => {
                    console.log(res, '创建用户');
                    if (res.data.code == 200) {
                      getUserInfo({
                        wechatMiniprogramOpenId: openid,
                        userId: '',
                        wechatServiceOfficialAccountOpenId: '',
                        wechatUnionId: ''
                      }).then(res => {
                        console.log(res, "创建过的用户信息");
                        SETSTORAGE('userId', res.data.data.userId)
                        // 跳转首页
                        wx.navigateTo({
                          url: `/pages/caidan1/caidanyi`,
                        })
                        return
                      })
                    }
                  })
                } else {
                  // 本地存储userId
                  SETSTORAGE('userId', res.data.data.userId)
                  const {
                    signedAgreementVersion,
                    currentAgreementVersion,
                    familyArchiveStatus
                  } = res.data.data
                  getPersonInfoApi({
                    userId: res.data.data.userId
                  }).then(res => {
                    console.log(res, '用户个人信息');
                    if (res.data.code == 200) {
                      SETSTORAGE('familyId', res.data.data.currentFamilyId)
                      SETSTORAGE('personId', res.data.data.personId)
                      SETSTORAGE('familyMemberId', res.data.data.currentFamilyMemberId)
                      SETSTORAGE('familyIds', res.data.data.familyIds)
                      const personId = res.data.data.personId
                      if (personId != null && res.data.data.currentFamilyMemberId == null && res.data.data.currentFamilyId == null) {
                        getFamilyMemberApi({
                          familyId: res.data.data.familyIds[0],
                          familyMemberId: ''
                        }).then(res => {
                          console.log(res, "家庭成员");
                          const list = res.data.data.filter(item => item.personalInfoId == personId && item.relation != null)
                          if (list.length > 0) {
                            wx.switchTab({
                              url: '/pages/index/index',
                            })
                            return
                          }
                        })
                      } else {
                        // 判断是否签订协议
                        if (signedAgreementVersion == currentAgreementVersion) {
                          // 判断建档状态
                          if (familyArchiveStatus == "empty") {
                            console.log(111);
                            wx.navigateTo({
                              url: '/pages/filing/filing',
                            })
                            return
                          } else if (familyArchiveStatus == "member") {
                            const userId = GETSTORAGE('userId')
                            // 获取用户个人信息
                            getPersonInfoApi({
                              userId: userId
                            }).then(res => {
                              if (res.data.code == 200) {
                                const {
                                  currentFamilyId
                                } = res.data.data
                                // 获取家庭成员信息
                                getFamilyMemberApi({
                                  familyId: currentFamilyId,
                                  familyMemberId: ''
                                }).then(res => {
                                  if (res.data.data.length > 1) {
                                    console.log('家庭关系');
                                    // 跳家庭关系页面
                                    wx.navigateTo({
                                      url: '/pages/filing/relationships/relationships',
                                    })
                                    return
                                  } else {
                                    console.log('家人');
                                    // 跳添加家人页面
                                    wx.navigateTo({
                                      url: '/pages/filing/family/family',
                                    })
                                    return
                                  }
                                })
                              }

                            })

                          } else if (familyArchiveStatus == "health_manage") {
                            wx.navigateTo({
                              url: '/pages/filing/health-management/healt-hmanagement',
                            })
                            return
                          } else {
                            wx.switchTab({
                              url: '/pages/index/index',
                            })
                            return
                          }
                        } else {
                          wx.navigateTo({
                            url: `/pages/caidan1/caidanyi`,
                          })
                          return
                        }
                      }

                    } else {
                      // 判断是否签订协议
                      if (signedAgreementVersion == currentAgreementVersion) {
                        // 判断建档状态
                        if (familyArchiveStatus == "empty") {
                          wx.navigateTo({
                            url: '/pages/filing/filing',
                          })
                          return
                        } else if (familyArchiveStatus == "member") {
                          const userId = GETSTORAGE('userId')
                          // 获取用户个人信息
                          getPersonInfoApi({
                            userId: userId
                          }).then(res => {
                            if (res.data.code == 200) {
                              const {
                                currentFamilyId
                              } = res.data.data
                              // 获取家庭成员信息
                              getFamilyMemberApi({
                                familyId: currentFamilyId,
                                familyMemberId: ''
                              }).then(res => {
                                if (res.data.data.length > 1) {
                                  console.log('家庭关系');
                                  // 跳家庭关系页面
                                  wx.navigateTo({
                                    url: '/pages/filing/relationships/relationships',
                                  })
                                  return
                                } else {
                                  console.log('家人');
                                  // 跳添加家人页面
                                  wx.navigateTo({
                                    url: '/pages/filing/family/family',
                                  })
                                  return
                                }
                              })
                            }

                          })

                        } else if (familyArchiveStatus == "health_manage") {
                          wx.navigateTo({
                            url: '/pages/filing/health-management/healt-hmanagement',
                          })
                          return
                        } else {
                          wx.switchTab({
                            url: '/pages/index/index',
                          })
                          return
                        }
                      } else {
                        wx.navigateTo({
                          url: `/pages/caidan1/caidanyi`,
                        })
                        return
                      }
                    }
                  })
                }
              })
            })
          }
        }
      })
    }
  },
  onShow: function () {
    updateManager();

  },
});